﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using EntityManage;
using BusinessFacade;

public partial class Modules_Personal_PerfNoticeAdd : System.Web.UI.Page
{
    IPerf_Personal_EvaluateHandler evaHandler = new Perf_Personal_EvaluateManage();
    IPerf_Personal_EvaluateHandlerExt evaExtHandler = new Perf_Personal_EvaluateManageExt();
    IPerf_Flow_TraceHandler flowTraceHandler = new Perf_Flow_TraceManage();
    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();
    IOther_NoticeHandler noticeHandler = new Other_NoticeManage();
    ISys_UserHandler userHandler = new Sys_UserManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("303040"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["UserPurviews"] != null && Session["UserPurviews"].ToString().Contains("303040"))
        {
            if ("".Equals(hdnOrgID.Value))
            {
                Jscript.Alert("请选择公示对象", this.Page);
                return;
            }

            int userId = int.Parse(Session["CurrentUserID"].ToString());

            List<string> ids = new List<string>();
            string[] evalIds = Request["aids"].Split(',');
            ids.AddRange(evalIds);

            if (ids.Count > 0)
            {
                foreach (string id in ids)
                {
                    Perf_Personal_ApprovalQuery approvalQuery = approvalHandler.GetPerf_Personal_ApprovalQuery(int.Parse(id));
                    if (approvalQuery != null)
                    {
                        approvalQuery.Status = (int)Enums.PersonalApprovalStatus.Notice;
                        approvalQuery.Notice_Len = int.Parse(txtNoticeLen.Text);
                        approvalQuery.Appeal_Len = int.Parse(txtAppealLen.Text);
                        approvalQuery.Create_Time = DateTime.Now;
                        approvalHandler.UpdatePerf_Personal_Approval(approvalQuery);

                        Perf_Personal_EvaluateQuery[] evaList = evaHandler.GetPerf_Personal_EvaluateQueryList(" Approval_ID = @Approval_ID", new SqlParameter[] { new SqlParameter("@Approval_ID", approvalQuery.ID) });
                        if (evaList != null && evaList.Length > 0)
                        {
                            foreach (Perf_Personal_EvaluateQuery evaQuery in evaList)
                            {
                                evaQuery.Status = (int)Enums.PersonalEvaluateStatus.Notice;
                                evaHandler.UpdatePerf_Personal_Evaluate(evaQuery);
                            }
                        }

                        // 添加公示信息
                        Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(approvalQuery.Target_User_ID);
                        Other_NoticeQuery notice = new Other_NoticeQuery();
                        notice.User_ID = userId;
                        notice.Title = approvalQuery.Title + " -- " + userQuery.Name;
                        notice.Notice_Range = int.Parse(hdnOrgID.Value);
                        notice.Content = "标题：" + approvalQuery.Title + "";
                        notice.Content += "<br/>人员：" + userQuery.Name;
                        notice.Content += "<br/>绩效日期：" + approvalQuery.Perf_Date.ToString();
                        notice.Content += "<br/>绩效得分：" + approvalQuery.Score.ToString();
                        notice.Content += "<br/>任务得分：" + approvalQuery.Task_Score.ToString();
                        notice.Content += "<br/>加分：" + approvalQuery.Plus_Score.ToString();
                        notice.Content += "<br/>加分原因：" + approvalQuery.Plus_Reason;
                        notice.Content += "<br/>公示天数：" + approvalQuery.Notice_Len.ToString();
                        notice.Content += "<br/>申诉天数：" + approvalQuery.Appeal_Len.ToString();

                        notice.Type = (int)Enums.OtherNoticeType.PersonalPerfNotice;
                        notice.Business_ID = approvalQuery.ID;
                        notice.Create_Date = DateTime.Now;
                        notice.Period = DateTime.Now.AddDays(double.Parse(txtNoticeLen.Text));

                        noticeHandler.InsertOther_Notice(notice);
                    }
                }
            }
        }

        Jscript.ReturnAndClose(this.Page, "true");
    }
}
